/**
 * description
 * <p>fill description</p>
 * @author zido
 * @since 2017/5/30 0030
 */

export const INCREASE = 'INCREASE'
export const DOUBLE_ASYNC = 'DOUBLE_ASYNC'

export const increase = (value = 1) => ({
  type: INCREASE,
  payload: value,
})

export const doubleAsync = () => {
  return (dispatch, getState) => {
    return new Promise(resolve => {
      setTimeout(() => {
        dispatch({
          type: DOUBLE_ASYNC,
          payload: getState().counter
        })
        resolve()
      }, 200)
    })
  }
}

export const actions = {
  increase,
  doubleAsync
}

const ACTION_HANDLERS = {
  [INCREASE]: (state, action) => state + action.payload,
  [DOUBLE_ASYNC]: (state) => state * 2
}

const initialState = 0

export const reducer = (state = initialState, action) => {
  const handler = ACTION_HANDLERS[action.type]

  return handler ? handler(state, action) : state
}

export default reducer


